package com.chixing.day20_annotation_jdk8.work;

import java.io.*;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class FileDemo {
    public static void main(String[] args) throws IOException {

        List<Coutomer> arrayList = new ArrayList<>();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
        LocalDateTime dateTime = LocalDateTime.parse("20060720200005",formatter);
        System.out.println(dateTime);

        BufferedReader br = null;
        try {
            br = new BufferedReader(new InputStreamReader(new FileInputStream("e:/test2/record.txt")));
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        }
        String s = "";
        while ((s =br.readLine())!=null){
            String[] s1 = s.split("\\|");
            System.out.println(Arrays.toString(s1));
            Coutomer coutomer = new Coutomer(s1[0],s1[1],s1[2],Integer.parseInt(s1[3]),Long.parseLong(s1[4]),LocalDateTime.parse(s1[5],formatter),Double.parseDouble(s1[6]));
            arrayList.add(coutomer);
        }
        br.close();
        arrayList.forEach(System.out::println);

        findByCustomerNumber(arrayList,"4155990188888888").forEach(System.out::println);
        findByDate(arrayList,"2004-07-25 20:00:05","2006-07-20 20:00:05").forEach(System.out::println);
        System.out.println(totalAmount(arrayList));

    }

    /**
     * 按客户号查询记录
     *
     * @param customerNumber - 客户号
     * @return 符合条件的记录集合或null
     */
    public static List<Coutomer> findByCustomerNumber(List<Coutomer> arrayList,String customerNumber) {
        return arrayList.stream().filter(coutomer -> coutomer.getCouId().equals(customerNumber)).collect(Collectors.toList());
    }

    /**
     * 按日期段查询记录
     *
     * @param startDate - 开始日期
     * @param endDate   - 结束日期
     * @return 符合条件的记录数组或null
     */
    public static List<Coutomer> findByDate(List<Coutomer> arrayList,String startDate, String endDate) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        return arrayList.stream().filter(coutomer -> coutomer.getCouTime().isAfter(LocalDateTime.parse(startDate,formatter))&&
                coutomer.getCouTime().isBefore(LocalDateTime.parse(endDate,formatter))).collect(Collectors.toList());
    }

    /**
     * 取得总金额
     * @return 总金额
     */
    public static double totalAmount(List<Coutomer> arrayList) {
        double n = arrayList.stream().map((cost) -> cost.getCouMoney()).reduce((sum,cost)->sum +cost).get();
        return n;
    }
}
